package com.ccp.dev.workflow.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.SysUserService;
import com.ccp.dev.workflow.dao.CommuReceiverDao;
import com.ccp.dev.workflow.model.CommuReceiver;
import com.ccp.dev.workflow.model.TaskOpinion;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * ClassDescribe:沟通接收人 Service
 * @author :jiayueqiao
 * Date: 2019-09-04
 * Since:1
 */
@Service
public class CommuReceiverService extends BaseService<CommuReceiver> {

    @Resource
    private CommuReceiverDao dao;
    @Resource
    private SysUserService sysUserService;
    /**
     * 设置沟通人员或流转人员的查看状态为已读。
     * @param taskEntity 任务实体
     * @param sysUser 用户实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void setCommuReceiverStatus(TaskEntity taskEntity, SysUser sysUser){
        String userId=sysUser.getUserId();
        String taskId=taskEntity.getId();
        //沟通意见任务、流转任务
        if(!TaskOpinion.STATUS_COMMUNICATION.toString().equals(taskEntity.getDescription()) &&
                !TaskOpinion.STATUS_TRANSTO.toString().equals(taskEntity.getDescription())) {
            return;
        }
        //当前人和任务执行人一致。
        if(!taskEntity.getAssignee().equals(userId)) {
            return;
        }
        CommuReceiver commuReceiver= dao.getByTaskReceiver(taskId, userId);
        //被通知人员为未读状态，则更新为已读。
        if(null!=commuReceiver){
            if(commuReceiver.getStatus().shortValue()==CommuReceiver.NOT_READ.shortValue()){
                commuReceiver.setStatus(CommuReceiver.READED);
                commuReceiver.setReceiveTime(new Date());
                dao.update(commuReceiver);
            }
        }
    }

    /**
     * 根据意见ID获取接收人。
     * @param opinionId 意见id
     * @return list
     */
    public List<CommuReceiver> getByOpinionId(String opinionId){
        return dao.getByOpinionId(opinionId);
    }

    /**
    * 保存接收人
    * @param opinionId 意见id
    * @param usrIdTaskIds 接收人id
    */
    @Transactional(rollbackFor = Exception.class)
    public void saveReceiver(String opinionId, Map<String, String> usrIdTaskIds, SysUser curUser) {
        Iterator<Map.Entry<String, String>> it = usrIdTaskIds.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> ent=it.next();
            String receiverId=ent.getKey();
            if(curUser.getUserId().equals(receiverId)) {
                continue;
            }
            String taskId=ent.getValue();
            SysUser sysUser= sysUserService.getById(receiverId);
            CommuReceiver comm=new CommuReceiver();
            comm.setId(UUIDUtils.getUUIDFor32());
            comm.setStatus(CommuReceiver.NOT_READ);
            comm.setOpinionId(opinionId);
            comm.setTaskId(taskId);
            Date date = new Date();
            comm.setReceiveTime(date);
            comm.setCreateTime(date);
            comm.setFeedbackTime(date);
            comm.setRecevierId(receiverId);
            comm.setReceiverName(sysUser.getFullName());
            dao.add(comm);
        }
    }

    /**
     * 设置沟通人员或流转人员的查看状态为已反馈。
     * @param taskEntity 任务实体
     * @param sysUser 用户实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void setCommuReceiverStatusToFeedBack(TaskEntity taskEntity,SysUser sysUser){
        String userId=sysUser.getUserId();
        String taskId=taskEntity.getId();
        //沟通意见任务、流转任务
        if(!TaskOpinion.STATUS_COMMUNICATION.toString().equals(taskEntity.getDescription()) &&
                !TaskOpinion.STATUS_TRANSTO.toString().equals(taskEntity.getDescription())) {
            return;
        }
        //当前人和任务执行人一致。
        if(!taskEntity.getAssignee().equals(userId)) {
            return;
        }
        CommuReceiver commuReceiver= dao.getByTaskReceiver(taskId, userId);
        if(null!=commuReceiver){
            //被通知人员为未读或已读状态，则更新为已反馈。
            if(commuReceiver.getStatus().shortValue()!=CommuReceiver.FEEDBACK.shortValue()){
                commuReceiver.setStatus(CommuReceiver.FEEDBACK);
                commuReceiver.setReceiveTime(new Date());
                dao.update(commuReceiver);
            }
        }
    }

    /**
     * 修改接收者状态
     * @param taskId 任务id
     * @param status 状态
     * @param recevierId 接收人id
     */
    @Transactional(rollbackFor = Exception.class)
    public void setCommuReceiverStatus(String taskId, Integer status, String recevierId){
        CommuReceiver commuReceiver = this.getByTaskReceiver(taskId, recevierId);
        if(BeanUtils.isEmpty(commuReceiver)){
            return;
        }
        commuReceiver.setStatus(status);
        commuReceiver.setFeedbackTime(new Date());
        dao.update(commuReceiver);
    }

    /**
     * 根据任务ID和接收人Id获取接收人信息。
     * @param taskId 任务id
     * @param recevierId 接收人id
     * @return CommuReceiver
     */
    private CommuReceiver getByTaskReceiver(String taskId, String recevierId){
        return dao.getByTaskReceiver(taskId, recevierId);
    }

    /**
     * 根据taskId进行删除
     * @param taskId 任务id
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByTaskId(String taskId) {
        dao.delByTaskId(taskId);
    }
}
